package org.colafries.metadata.convert;

import org.colafries.metadata.entity.enums.ColumnTypeEnum;
import com.google.common.base.Strings;
import org.colafries.common.metadata.Column;
import org.colafries.common.utils.DateTimeUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;

/**
 * @author colafries
 * @version v1.0
 * @ClassName ITypeConvert
 * @Description
 * @date Created in 2024/3/24
 **/
public interface ITypeConvert {
    ColumnTypeEnum convert(Column column);


    /***
     * @param results ResultSet
     * @param columnName 字段名
     * @param columnType 字段类型
     * @return java.lang.Object
     * @author colafries
     * @Date 2024/3/25
     */
    default Object convertValue(ResultSet results, String columnName, String columnType) throws SQLException {
        if (Strings.isNullOrEmpty(columnType)) {
            return results.getString(columnName);
        }
        switch (columnType.toLowerCase()) {
            case "string":
            case "text":
            case "varchar":
                return results.getString(columnName);
            case "double":
                return results.getDouble(columnName);
            case "int":
                return results.getInt(columnName);
            case "float":
                return results.getFloat(columnName);
            case "bigint":
                return results.getLong(columnName);
            case "decimal":
                return results.getBigDecimal(columnName);
            case "date":
            case "localdate":
                return results.getDate(columnName);
            case "time":
            case "localtime":
                return results.getTime(columnName);
            case "datetime":
            case "timestamp":
                LocalDateTime dateTime = DateTimeUtil.toLocalDateTime(results.getTimestamp(columnName));
                return DateTimeUtil.formatDateTime(dateTime);
            case "blob":
                return results.getBlob(columnName);
            case "boolean":
            case "bool":
            case "bit":
                return results.getBoolean(columnName);
            case "byte":
                return results.getByte(columnName);
            case "bytes":
                return results.getBytes(columnName);
            default:
                return results.getString(columnName);
        }
    }

}
